<?php

namespace App\Http\Controllers;

use App\Http\Requests\LiveRequest;
use App\Models\Activity;
use App\Models\LiveUser;
use App\Models\User;
use Illuminate\Http\Request;
use \DB;
use App\Http\Controllers\Controller;
use App\Models\Live;

class LiveController extends CommonController
{
    /**
     * 获取列表
     */
    public function getIndex()
    {
        $lists = Live::orderBy('created_at', 'desc')->paginate(10);

        return view('live.index')->with('lists', $lists)->with('actions', $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();

        $sql = Live::orderBy('id', 'asc');

        if (isset($data['subject']) && $data['subject'] != '') {
            $sql->where('subject', 'like', '%' . $data['subject'] . '%');
        }

        if (isset($data['begin_time']) && $data['begin_time'] != '') {
            $data['begin_time'] = strtotime($data['begin_time']);
            $sql->where('begin_time', '>', $data['begin_time']);
        }

        if (isset($data['end_time']) && $data['end_time'] != '') {
            $data['end_time'] = strtotime($data['end_time']);
            $sql->where('end_time', '<', $data['end_time']);
        }

        if (isset($data['trailer_time']) && $data['trailer_time'] != '') {
            $data['trailer_time'] = strtotime($data['trailer_time']);
            $sql->where('trailer_time', '>', $data['trailer_time']);
        }

        $lists = $sql->paginate(10);

        return view('live.search')->with("lists", $lists)->with("subject", $data['subject'])
            ->with("begin_time", $data['begin_time'])->with("trailer_time", $data['trailer_time'])
            ->with("end_time", $data['end_time'])->with("actions", $this->actions);
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        return view('live.create');
    }

    public function postFindActivity(Request $request)
    {
        $data = $request->input();
        $lists = Activity::where('subject', 'like', '%' . trim($data['activity_search']) . '%')->get();

        return AjaxCallbackMessage($lists, true);
    }

    /**
     * 执行一个创建
     */
    public function postCreate(LiveRequest $request)
    {
        $data = $request->input();
        $vips = $data['vips'];
        $vips = explode(',', str_replace('，',',', $vips));
        //剔除可能出现的连续两个逗号
        $vips = array_filter($vips, function($v){if($v==''){ return false;} else{ return true;}});
        $vips = array_map(function($v){return trim($v);}, $vips);
        foreach($vips as $k=>$v){
            $user = User::where('username', $v)->first();
            if(!is_object($user)){
                return AjaxCallbackMessage('你输入的username，至少有一个系统中不存在此用户，请核对',false);
            }
        }

        $activity_id = $data['activity_id'];
        unset($data['formType'], $data['_token'], $data['activity_id'], $data['activity_search'], $data['vips']);
        $data['begin_time'] = strtotime($data['begin_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['trailer_time'] = strtotime($data['trailer_time']);
        $live = Live::create($data);

        foreach($vips as $k=>$v){
            $vip = User::where('username', $v)->first();
            LiveUser::create(['live_id'=>$live->id, 'user_id'=>$vip->uid]);
        }

        if($activity_id){
            $url = $this->_appurl . '/live/detail?pagetag=live-01&id=' . $live->id . '&activity_id=' . $activity_id;
            Live::where('id', $live->id)->update(['url' => $url]);
        }


        return AjaxCallbackMessage('创建成功', true, url('live/index'));
    }

    /**
     * 获取一个编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = Live::where('id', $data['id'])->first();

        $position = strpos($list->url, "activity_id=");
        if ($position) {
            $position += 12;
            $id = substr('http://wj3t.wjtr.com/webapp/live/detail?id=13?&activity_id=66', $position);
            $activity = Activity::where('id', $id)->first();
        }else{
            $activity = null;
        }

        $vips = LiveUser::where('live_id', $data['id'])->get();
        $usernames = array();
        foreach($vips as $k=>$v){
            $vip = User::where('uid', $v->user_id)->first();
            $usernames[]=$vip->username;
        }

        $usernames = implode(',', $usernames);

        return view('live.edit')->with('list', $list)->with('activity', $activity)->with('usernames', $usernames);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(LiveRequest $request)
    {
        $data = $request->input();
        $vips = $data['vips'];
        $vips = explode(',', str_replace('，',',', $vips));
        //剔除可能出现的连续两个逗号
        $vips = array_filter($vips, function($v){if($v==''){ return false;} else{ return true;}});
        $vips = array_map(function($v){return trim($v);}, $vips);

        foreach($vips as $k=>$v){
            $user = User::where('username', $v)->first();
            if(!is_object($user)){
                return AjaxCallbackMessage('你输入的username，至少有一个系统中不存在此用户，请核对',false);
            }
        }

        $id = $data['id'];

        $activity_id = $data['activity_id'];
        unset($data['formType'], $data['_token'], $data['id'], $data['activity_search'], $data['activity_id'], $data['vips']);
        $data['begin_time'] = strtotime($data['begin_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['trailer_time'] = strtotime($data['trailer_time']);

        Live::where('id', $id)->update($data);

        if($activity_id){
            $url = $this->_appurl . '/live/detail?pagetag=live-01&id=' .$id . '&activity_id=' . $activity_id;
            Live::where('id', $id)->update(['url'=>$url]);
        }

        LiveUser::where('live_id', $id)->delete();
        foreach($vips as $k=>$v){
            $vip = User::where('username', $v)->first();
            LiveUser::create(['live_id'=>$id, 'user_id'=>$vip->uid]);
        }

        return AjaxCallbackMessage('编辑成功', true, url('live/index'));
    }

    /**
     * 执行一个删除
     */
    public function postDelete(Request $request)
    {
        $data = $request->input();
        Live::where('id', $data['lid'])->delete();
        LiveUser::where('live_id', $data['lid'])->delete();

        return AjaxCallbackMessage('删除成功', true, url('live/index'));
    }
}
